# -*- coding: utf-8 -*-
import fanfou

import unittest

class FanfouTestCase(unittest.TestCase):
    def setUp(self):
        self.f = fanfou.Fanfou()
        
    def tearDown(self):
        self.f = None
    
    def testOne(self):
        """A sample test case."""
        self.assertEqual(self.f.one(), True, "Msg")
        
    def testGetUrlPage(self):
        """test Fanfou.getUrlPage"""
        self.assertNotEqual(self.f.getUrlPage("http://api.fanfou.com/statuses/public_timeline.xml"), 
                            "", "get URL page unsuccessfully")

    def testParseTimeLine(self):
        """test Fanfou.parseTimeLine"""
        h = open(r"testdata\statuses_user_timeline.xml", 'r')
        data = h.read()
        h.close()
        c = self.f.parseTimeLine(data)
        
        s = [fanfou.Status(created_at=u"Thu Mar 06 02:27:26 +0000 2008",
                           id=u"QAXTfesN0dE",
                           text=u"太闲了，没劲，找点有意思的干。", 
                           user=fanfou.User(screen_name=u"A.TNG",
                                       id=u"jiyucn",
                                       name=u"A.TNG",
                                       location=u"北京 海淀区",
                                       description=u"搞IT的，喜欢Python，喜欢看美剧，喜欢音乐，理想是成为IT界/厨艺界/音乐界的高手。",
                                       profile_image_url=u"http://avatar.fanfou.com/s0/00/36/3p.jpg?1199597003",
                                       url=u"http://fanfou.com/jiyucn",
                                       protected=u"false")),
             fanfou.Status(created_at=u"Tue Mar 04 02:54:56 +0000 2008",
                           id=u"XS_ncMWSY_w",
                           text=u"Android SDK update: m5-rc15 released", 
                           user=fanfou.User(screen_name=u"A.TNG",
                                       id=u"jiyucn",
                                       name=u"A.TNG",
                                       location=u"北京 海淀区",
                                       description=u"搞IT的，喜欢Python，喜欢看美剧，喜欢音乐，理想是成为IT界/厨艺界/音乐界的高手。",
                                       profile_image_url=u"http://avatar.fanfou.com/s0/00/36/3p.jpg?1199597003",
                                       url=u"http://fanfou.com/jiyucn",
                                       protected=u"false"))]
        self.assertEqual(c, s, "parseTimeLine returned value is not equal with standard one")
    
    def testParseStatus(self):
        """test Fanfou.parseStatus"""
        h=open(r"testdata\statuses_show.xml", 'r')
        data = h.read()
        h.close()
        c = self.f.parseStatus(data)
        
        s = fanfou.Status(created_at=u"Thu Mar 06 02:27:26 +0000 2008", 
                          id=u"QAXTfesN0dE", 
                          text=u"太闲了，没劲，找点有意思的干。", 
                          user=fanfou.User(screen_name=u"A.TNG", 
                                           id=u"jiyucn", 
                                           name=u"A.TNG", 
                                           location=u"北京 海淀区", 
                                           description=u"搞IT的，喜欢Python，喜欢看美剧，喜欢音乐，理想是成为IT界/厨艺界/音乐界的高手。", 
                                           profile_image_url=u"http://avatar.fanfou.com/s0/00/36/3p.jpg?1199597003", 
                                           url=u"http://fanfou.com/jiyucn", 
                                           protected=u"false"))
        self.assertEqual(c, s, "parseStatus returned value is not equal with standard one")
        
    def testParseUser(self):
        """test Fanfou.parseUser"""
        h=open(r"testdata\users_show.xml", 'r')
        data = h.read()
        h.close()
        c = self.f.parseUser(data)
        
        s = fanfou.User(screen_name=u"A.TNG", 
                        id=u"jiyucn", 
                        name=u"A.TNG", 
                        location=u"北京 海淀区", 
                        description=u"搞IT的，喜欢Python，喜欢看美剧，喜欢音乐，理想是成为IT界/厨艺界/音乐界的高手。", 
                        profile_image_url=u"http://avatar.fanfou.com/s0/00/36/3p.jpg?1199597003", 
                        url=u"http://fanfou.com/jiyucn", 
                        protected=u"false",
                        friends_count=6, 
                        followers_count=5, 
                        favourites_count=0,
                        statuses_count=85, 
                        utc_offset=28800, 
                        status=fanfou.Status(created_at=u"Thu Mar 06 02:27:26 +0000 2008", 
                                             id=u"QAXTfesN0dE", 
                                             text=u"太闲了，没劲，找点有意思的干。"))
        self.assertEqual(c, s, "parseUser returned value is not equal with standard one")
    
    def testParseUsers(self):
        """test Fanfou.parseUsers"""
        h = open(r"testdata\users.xml", 'r')
        data = h.read()
        h.close()
        userList = self.f.parseUsers(data)
        
        stdList = [fanfou.User(screen_name=u"Leij.", 
                               id=u"leij", 
                               name=u"Leij.", 
                               location=u"", 
                               description=u"", 
                               profile_image_url=u"http://avatar.fanfou.com/s0/00/00/00.jpg", 
                               url=u"http://fanfou.com/leij", 
                               protected=u"false"),
                   fanfou.User(screen_name=u"随遇而安", 
                               id=u"adaylee", 
                               name=u"随遇而安", 
                               location=u"", 
                               description=u"", 
                               profile_image_url=u"http://avatar.fanfou.com/s0/00/00/00.jpg", 
                               url=u"http://fanfou.com/adaylee", 
                               protected=u"true")]
        self.assertEqual(userList, stdList, "parseUsers returned list is not equal with the standard one")

    #def testPrivateMessagesInbox(self):
    #    """test Fanfou.privateMessagesInbox"""
    #    li = self.f.privateMessagesInbox("tJKX9EFPBIc")
    #    self.assertTrue(isinstance(li, list), "get private message from inbox failed")
    #    for i in li:
    #        self.assertTrue(isinstance(i, fanfou.PrivateMessage), 
    #                        "type of private message error")
    
    def testStatusesFriendsTimeLine(self):
        """test Fanfou.statusesFriendsTimeLine"""
        c = self.f.getFriendsTimeLine("jiyucn", 10)
        self.assertTrue(isinstance(c, list), "get friends time line failed")
        self.assertTrue((len(c) <= 10), "number of item is bigger than 10")
        for i in c:
            self.assertTrue(isinstance(i, fanfou.Status), "item type error")
    
    def testStatusesPublicTimeLine(self):
        """test Fanfou.statusesPublicTimeLine"""
        c = self.f.getPublicTimeLine(10)
        self.assertTrue(isinstance(c, list), "get public time line failed")
        self.assertTrue((len(c) <= 10), "number of item is bigger than 10")
        for i in c:
            self.assertTrue(isinstance(i, fanfou.Status), "item type error")
    
    def testStatusesShow(self):
        """test Fanfou.statusesShow"""
        s = self.f.getStatus("tJKX9EFPBIc")
        self.assertTrue(isinstance(s, fanfou.Status), "get specific message failed")
        self.assertTrue(isinstance(s.user, fanfou.User), "user type error")
    
    def testStatusesUserTimeLine(self):
        """test Fanfou.statusesUserTimeLine"""
        c = self.f.getUserTimeLine("jiyucn", 10)
        self.assertTrue(isinstance(c, list), "get user time line failed")
        self.assertTrue((len(c) <= 10), "number of item is bigger than 10")
        for i in c:
            self.assertTrue(isinstance(i, fanfou.Status), "item type error")
    
    def testUsersFollows(self):
        """test Fanfou.usersFollows"""
        li = self.f.getFollows("jiyucn")
        self.assertTrue(isinstance(li, list), "get user follows failed")
        for i in li:
            self.assertTrue(isinstance(i, fanfou.User), "user type error")
    
    def testUsersFriends(self):
        """test Fanfou.usersFriends"""
        li = self.f.getFriends("jiyucn")
        self.assertTrue(isinstance(li, list), "get user friends failed")
        for i in li:
            self.assertTrue(isinstance(i, fanfou.User), "user type error")
    
    def testUsersShow(self):
        """test Fanfou.usersShow"""
        u = self.f.getUser("jiyucn")
        self.assertTrue(isinstance(u, fanfou.User), "get user info failed")
    
class FanfouTestSuite(unittest.TestSuite):
    def __init__(self):
        unittest.TestSuite.__init__(self,map(FanfouTestCase,
                                             ("testOne", 
                                              "testGetUrlPage",
                                              "testPrivateMessagesInbox",
                                              "testStatusesFriendsTimeLine",
                                              "testStatusesPublicTimeLine",
                                              "testStatusesShow",
                                              "testStatusesUserTimeLine",
                                              "testUsersFollows",
                                              "testUsersFriends",
                                              "testUsersShow",
                                              "testParseTimeLine",
                                              "testParseUsers",)))

if __name__ == "__main__":
    unittest.main()
